﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Entities;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    public class WageController : Controller
    {
        private IWageRepository wageRepository;
        private IEmployeeRepository employeeRepository;
        private IUnitOfWork unitOfWork;
        private IInsureRepository insureRepository;
        public WageController(IWageRepository wageRepository,
            IEmployeeRepository employeeRepository,
            IInsureRepository insureRepository,
            IUnitOfWork unitOfWork)
        {
            this.wageRepository = wageRepository;
            this.employeeRepository = employeeRepository;
            this.insureRepository = insureRepository;
            this.unitOfWork = unitOfWork;
        }

        public IList<WageModelOutline> EmployeeToWageModelOutline()
        {
            //给员工设置工资,已经离职的员工不能进行工资设定
            var employees = employeeRepository.GetMany(e => e.EmployeeStatus != "离职状态");
            IList<WageModelOutline> wageModelOutlines = new List<WageModelOutline>();
            foreach (Employee e in employees)
            {
                wageModelOutlines.Add(new WageModelOutline()
                {
                    EmployeeId = e.ID,
                    Name = e.Name,
                    Department = e.Department.Name,
                    Post = e.Post.Title,
                    BasicWage = e.BasicWage,
                    BasicValue = e.BasicValue
                });
            }
            return wageModelOutlines;
        }
        //工资设置的起始页面
        public PartialViewResult WageSetIndex()
        {
            return PartialView(EmployeeToWageModelOutline());
        }
        [GridAction]
        public ActionResult WageSetIndexAjax()
        {
            return View(new GridModel(EmployeeToWageModelOutline()));
        }
        //工资设定的HttpGet页面
        [HttpGet]
        public PartialViewResult WageSet(int id)
        {
            //获取员工
            Employee employee = employeeRepository.GetById(id);
            //工资视图模型
            WageModelDetial wageModelDetial;
            if (employee != null)
            {
                wageModelDetial = new WageModelDetial()
                                      {
                                          EmployeeId = employee.ID,
                                          Name = employee.Name,
                                          BasicWage = employee.BasicWage,
                                          BasicValue = employee.BasicValue,
                                          Remark = employee.WageRemark
                                      };
            }
            else
            {
                wageModelDetial = new WageModelDetial();
            }
            return PartialView(wageModelDetial);
        }
        //工资设定的HttpPost页面
        [HttpPost]
        public RedirectToRouteResult WageSet(WageModelDetial wageModelDetial)
        {
            Employee employee = employeeRepository.GetById(wageModelDetial.EmployeeId);
            if (ModelState.IsValid & employee != null)
            {
                employee.BasicWage = wageModelDetial.BasicWage;
                employee.BasicValue = wageModelDetial.BasicValue;
                employee.WageRemark = wageModelDetial.Remark;

                employeeRepository.Update(employee);
                unitOfWork.Commit();
            }
            return RedirectToAction("Index", "HumanResources");
        }

        //工资发放登记的HttpPost页面
        [HttpGet]
        public PartialViewResult WageGrant(int id)
        {
            //获取员工
            Employee employee = employeeRepository.GetById(id);
            //获取奖金基数的数据记录的第一条记录
            Insure insure = insureRepository.GetById(1);
            if (insure == null)
            {
                //无数据,添加一条默认数据,并重新获取
                insureRepository.Add(new Insure() { Id = 1 });
                unitOfWork.Commit();
                insure = insureRepository.GetById(1);
            }
            //工资视图模型
            WageModelDetial wageModelDetial;
            if (employee != null)
            {
                wageModelDetial = new WageModelDetial()
                {
                    EmployeeId = employee.ID,
                    Name = employee.Name,
                    BasicWage = employee.BasicWage,
                    BasicValue = employee.BasicValue,
                    //五险一金(基数*比率)
                    EndowmentAssurance = employee.BasicValue * insure.EndowmentAssurance,
                    MedicalInsurance = employee.BasicValue * insure.MaternityInsurance,
                    UnemploymentInsurance = employee.BasicValue * insure.UnemploymentInsurance,
                    EmploymentInjuryInsurance = employee.BasicValue * insure.EmploymentInjuryInsurance,
                    MaternityInsurance = employee.BasicValue * insure.MaternityInsurance,
                    ProvidentFund = employee.BasicValue * insure.ProvidentFund,

                    Remark = employee.WageRemark
                };
            }
            else
            {
                wageModelDetial = new WageModelDetial();
            }
            return PartialView(wageModelDetial);
        }
        //工资发放的HttpPost页面
        [HttpPost]
        public RedirectToRouteResult WageGrant(WageModelDetial wageModelDetial)
        {
            Wage wage = new Wage();
            if (ModelState.IsValid)
            {
                wage.EmployeeId = wageModelDetial.EmployeeId;

                wage.BasicWage = wageModelDetial.BasicWage;
                wage.BasicValue = wageModelDetial.BasicValue;
                wage.EndowmentAssurance = wageModelDetial.EndowmentAssurance;
                wage.MedicalInsurance = wageModelDetial.MedicalInsurance;
                wage.UnemploymentInsurance = wageModelDetial.UnemploymentInsurance;
                wage.EmploymentInjuryInsurance = wageModelDetial.EmploymentInjuryInsurance;
                wage.MaternityInsurance = wageModelDetial.MaternityInsurance;
                wage.ProvidentFund = wageModelDetial.ProvidentFund;

                wage.TakeOff = wageModelDetial.TakeOff;
                wage.Subsidy = wageModelDetial.Subsidy;

                wage.WageDay = wageModelDetial.WageDay;
                wage.Remark = wageModelDetial.Remark;

                wageRepository.Add(wage);
                unitOfWork.Commit();
            }
            return RedirectToAction("Index", "HumanResources");
        }
        //检查员工当月工资是否添加到工资记录表中
        [HttpPost]
        public JsonResult CheckIn(string id, string wageDate)
        {
            int employeeId = Convert.ToInt32(id);
            DateTime time = Convert.ToDateTime(wageDate);
            var wageModels = wageRepository.GetMany(w => w.EmployeeId == employeeId
                & w.WageDay.Month == time.Month);

            var b = wageModels.Count() == 0 ? "true" : "false";

            JsonResult json = new JsonResult()
            {
                Data = new { Value = b }
            };
            return Json(json);

        }

        //将Wage模型转化为WageModelDetial视图实体模型
        public IList<WageModelDetial> WageToWageModelDetial()
        {
            var wages = wageRepository.GetAll().AsQueryable();
            return (from w in wages
                    select new WageModelDetial()
                               {
                                   Id = w.ID,
                                   EmployeeId = w.EmployeeId,
                                   Name = w.Employee.Name,
                                   BasicWage = w.BasicWage,
                                   BasicValue = w.BasicValue,
                                   EndowmentAssurance = w.EndowmentAssurance,
                                   MedicalInsurance = w.MedicalInsurance,
                                   UnemploymentInsurance = w.UnemploymentInsurance,
                                   EmploymentInjuryInsurance = w.EmploymentInjuryInsurance,
                                   MaternityInsurance = w.MaternityInsurance,
                                   ProvidentFund = w.ProvidentFund,
                                   TakeOff = w.TakeOff,
                                   Subsidy = w.Subsidy,
                                   WageDay = w.WageDay
                               })
                    .ToList();

        }
        //管理工资发放数据的起始页面
        public PartialViewResult WageManageIndex()
        {
            return PartialView(WageToWageModelDetial());
        }
        //管理考勤数据,Telerik的分页及刷新
        [GridAction]
        public ActionResult WageManageAjax()
        {
            return View(new GridModel(WageToWageModelDetial()));
        }
        //Telerik的删除Wage数据
        [HttpPost]
        [GridAction]
        public ActionResult WageManageDelete(int id)
        {
            Wage wage = wageRepository.GetById(id);

            if (wage != null)
            {
                wageRepository.Delete(wage);
            }

            //保存
            unitOfWork.Commit();

            //重新绑定grid
            return View(new GridModel(WageToWageModelDetial()));
        }
    }
}
